<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Myencrypt
{
	private static $code = "tncdMSBq7x4R3ITubgAe1Fy-jiPEk0GpZCOrzlY8VDhH2m9LaQs6X_UfJ5WwKNov";
	private static function _shiftLeft($pos, $shiftStep)
	{
		$charCodeCount = strlen(self::$code);

		if (($pos + $shiftStep) > ($charCodeCount - 1))
		{
			$pos = ($pos + $shiftStep) - $charCodeCount;
		}
		else
		{
			$pos = $pos + $shiftStep;
		}
		return $pos;
	}

	private static function _shiftRight($pos, $shiftStep)
	{
		$charCodeCount = strlen(self::$code);
		if ($pos - $shiftStep < 0)
		{
			$pos = $charCodeCount - ($shiftStep - $pos);
		}
		else
		{
			$pos = $pos - $shiftStep;
		}
		return $pos;
	}
	
	
	public static function Encode($inputID)
	{
		$output = "";
        $id = $inputID;
        $shift = $inputID%64;
        $quotient = $id;
		do
		{
			$id       = $quotient % 64;
			$quotient = $quotient >> 6; // divide by 64
			$output   = self::$code[$id] . $output;
		}
		while ($quotient>0);
		return $output;
	}
    public static function Decode($inputID)
	{
        $output = 0;
        $len = strlen($inputID);
        $i;
		for ($i=0; $i<$len; $i++)
		{
            $value = strpos(self::$code, $inputID[$i]);
			$output += $value*pow(64,$len-$i-1);
		}
		return $output;
	}
	
	/*
	public static function Encrypt($vID, $strLink)
	{
		$result = "";
		$shift = $vID % 64;
		$strLinkLen = strlen($strLink);
		$i = 0;
		for ($i = 0; $i < $strLinkLen; $i++)
		{
			$asciiPos = $strLink[$i];
			$pos = $asciiPos % 64;
			$diviPos = ($asciiPos - $pos) / 64;
			$result .= $charlinkcode[self::_shiftLeft($diviPos, $shift)] . $charlinkcode[self::_shiftLeft($pos, $shift)];
		}
		return $charlinkcode[$shift] . $result;
	}

	public static function Decrypt($strLink)
	{
		$result = "";
		$shift = $charlinkcode.IndexOf($strLink[0]);

		$strLink = $strLink.Substring(1);
		$strLinkLen = $strLink.Length;
		$i = 0;
		while ($i < $strLinkLen)
		{
			$subCode = strLink.Substring(i, 2);
			$pos = $charlinkcode.IndexOf(charlinkcode[shiftRight(charlinkcode.IndexOf(subCode[1]), shift)]);
			$diviPos = charlinkcode.IndexOf(charlinkcode[shiftRight(charlinkcode.IndexOf(subCode[0]), shift)]);
			$result .= Convert.ToChar(diviPos * 64 + pos);
			i += 2;
		}
		return result;
	}
	*/
}